﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using smsclictr.automation;
using Opalis.QuickIntegrationKit;
using System.Management;

namespace OpalisAddOn.DDR
{
    [OpalisObject("New Computer DDR", Description = "Create DDRFile content to create a new Computer", ShowFilters=false, ShowInputs=true)]
    public class NewComputerDDR : IOpalisObject
    {
        public void Design(IOpalisDesigner designer)
        {
            designer.AddInput("SiteCode");
            designer.AddInput("Computer Name");
            designer.AddInput("TargetFile").WithFileBrowser();
            designer.AddInput("MAC Address").NotRequired();
            designer.AddInput("SMBIOS GUID").NotRequired();

            designer.AddInput("AD Site Name").NotRequired();
            designer.AddInput("Location").NotRequired();
            designer.AddInput("OS").NotRequired();
            designer.AddInput("Role").NotRequired();
            designer.AddInput("CreateNewGUID").NotRequired().WithBooleanBrowser();
        }

        public void Execute(IOpalisRequest request, IOpalisResponse response)
        {
            smsclictr.automation.DDRFile newDDR = new DDRFile("System", "Opalis Discovery",  request.Inputs["SiteCode"].AsString().ToUpper());

            newDDR.DDRAddString("Name", request.Inputs["Computer Name"].AsString(), 16, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NAME | DDRFile.DDRPropertyFlagsEnum.ADDPROP_KEY);
            newDDR.DDRAddString("Netbios Name", request.Inputs["Computer Name"].AsString(), 16, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NAME);
            newDDR.DDRAddStringArray("SMS Assigned Sites", new string[] { request.Inputs["SiteCode"].AsString().ToUpper() }, 16, DDRFile.DDRPropertyFlagsEnum.ADDPROP_REPLACE);
            
            if(request.Inputs.Contains("CreateNewGUID"))
            {
                if (Boolean.Parse(request.Inputs["CreateNewGUID"].AsString()))
                {
                    newDDR.DDRAddString("SMS Unique Identifier", "GUID:" + System.Guid.NewGuid().ToString(), 42, DDRFile.DDRPropertyFlagsEnum.ADDPROP_GUID | DDRFile.DDRPropertyFlagsEnum.ADDPROP_KEY);
                }
            }

            if (request.Inputs.Contains("MAC Address"))
            {
                string sMAC = request.Inputs["MAC Address"].AsString();

                if (sMAC.Length == 12)
                {
                    sMAC = sMAC.Insert(2, "-");
                    sMAC = sMAC.Insert(5, "-");
                    sMAC = sMAC.Insert(8, "-");
                    sMAC = sMAC.Insert(11, "-");
                    sMAC = sMAC.Insert(14, "-");
                }
                sMAC = sMAC.Replace('-', ':');
                sMAC = sMAC.ToUpper();

                newDDR.DDRAddStringArray("MAC Addresses", new string[] { sMAC }, 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_KEY);

            }

            //Assign Optional strings...
            if (request.Inputs.Contains("AD Site Name"))
                newDDR.DDRAddString("AD Site Name", request.Inputs["AD Site Name"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("Location"))
                newDDR.DDRAddString("Location", request.Inputs["Location"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("OS"))
                newDDR.DDRAddString("Operating System Name and Version", request.Inputs["OS"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("Role"))
                newDDR.DDRAddString("Role", request.Inputs["Role"].AsString(), 64, DDRFile.DDRPropertyFlagsEnum.ADDPROP_NONE);
            if (request.Inputs.Contains("SMBIOS GUID"))
                newDDR.DDRAddString("SMBIOS GUID", request.Inputs["SMBIOS GUID"].AsString(), 38, DDRFile.DDRPropertyFlagsEnum.ADDPROP_KEY);

            newDDR.DDRWrite(request.Inputs["TargetFile"].AsString());
        }
    }
}
